package com.jellyfishumbrella;

import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;

import org.lwjgl.LWJGLException;

import com.jellyfishumbrella.GL.RendererGL;

public class VoronoiLandscapeMain {
	private static boolean		do3D		= true;
	private static final String	LOOKANDFEEL	= "System";

	GameLogic					gameLogic;
	//GamePanel					gamePanel;
	JTextField					txtDebug;

	RendererGL					displayGL;
	long						lastFrame;
	int							fps;
	long						lastFPS;

	public static void main(String[] args) {
		setupUISkin();

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VoronoiLandscapeMain frame = new VoronoiLandscapeMain();
					// frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void setupUISkin() {
		String lookAndFeel = null;

		if (LOOKANDFEEL != null) {
			if (LOOKANDFEEL.equals("Metal")) {
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
				// an alternative way to set the Metal L&F is to replace the
				// previous line with:
				// lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";

			}

			else if (LOOKANDFEEL.equals("System")) {
				lookAndFeel = UIManager.getSystemLookAndFeelClassName();
			}

			else if (LOOKANDFEEL.equals("Motif")) {
				lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			}

			else if (LOOKANDFEEL.equals("GTK")) {
				lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
			}

			else {
				System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL);
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
			}

			try {

				UIManager.setLookAndFeel(lookAndFeel);

			}

			catch (ClassNotFoundException e) {
				System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel);
				System.err.println("Did you include the L&F library in the class path?");
				System.err.println("Using the default look and feel.");
			}

			catch (UnsupportedLookAndFeelException e) {
				System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform.");
				System.err.println("Using the default look and feel.");
			}

			catch (Exception e) {
				System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason.");
				System.err.println("Using the default look and feel.");
				e.printStackTrace();
			}
		}
	}

	/**
	 * Create the frame.
	 */
	public VoronoiLandscapeMain() {
		if (do3D) {
			createGUI3D();
		}
		gameLogic = new GameLogic();
		if (do3D) {
			try {
				displayGL.connectToLogic(gameLogic);
			} catch (LWJGLException e) {
				e.printStackTrace();
			}
		}
		gameLogic.init();
		gameLogic.gameLoop();
	}

	public void createGUI3D() {
		try {
			displayGL = new RendererGL();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}

	public class MouseZoomListener implements MouseWheelListener {

		@Override
		public void mouseWheelMoved(MouseWheelEvent e) {
			int notches = e.getWheelRotation();
			System.out.println(notches + "   " + GameLogic.SCALING);
			if (notches < 0) {
				gameLogic.renderGL.camera.FOV /= 2.0;
			} else {
				gameLogic.renderGL.camera.FOV *= 2.0;
			}
			//gamePanel.repaint();
		}
	}
}
